package com.city.ajax.business;


import com.city.config.Code;
import com.city.domain.city.CityEventType;
import com.city.domain.city.CityTown;
import com.city.domain.city.CityVillage;
import com.city.domain.pub.PubUser;
import com.city.domain.report.CtDoBan;
import com.city.domain.report.CtMessage;
import com.city.domain.report.CtReport;
import com.city.domain.report.format.CtUserFoemat;
import com.city.format.CtDoBanFormat;
import com.city.format.CtReportFormat;
import com.city.helper.AuthHelper;
import com.city.helper.EntityHelper;
import com.city.helper.FormatHelper;
import com.city.repository.city.CityEventTypeRepository;
import com.city.repository.city.CityTownRepository;
import com.city.repository.city.CityVillageRepository;
import com.city.repository.pub.PubUserRepository;
import com.city.repository.report.CtReportRepository;
import com.city.repository.report.CtUserBranchRepository;
import com.city.repository.report.format.CtUserFoematRepository;
import com.city.service.pub.PubUserService;
import com.city.service.report.MessageService;
import com.city.service.report.ReportService;
import com.city.service.report.UserService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @deprecation: 业务处置ajax请求
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/29 18:29
 */
@RestController
@RequestMapping("/ajax/business")
public class BusinessAjax {

    @Autowired
    private ReportService reportService;
    @Autowired
    private EntityHelper entityHelper;
    @Autowired
    private FormatHelper formatHelper;
    @Autowired
    private AuthHelper authHelper;
    @Autowired
    private PubUserService pubUserService;
    @Autowired
    private MessageService messageS;
    @Autowired
    private CtUserFoematRepository ctUserFoematRepository;

    /**
     * @param id      Integer 案件id
     * @param content String 反馈内容
     * @return Map
     * @deprecation 业务处置反馈
     * @author 雷楚桥
     * @date 2018-08-12 16:15
     */
    @ResponseBody
    @RequestMapping("/feedback")
    public Map<String, Object> feedback(@Param("id") Integer id,
                                        @Param("content") String content) {
        Map<String, Object> map = new HashMap<>();

        CtReport report = reportService.findReportById(id);
        if (report != null) {
            report.setComment(content);
            CtReport data = reportService.saveCtReport(report);
            map.put("code", Code.SUCCESS);
            map.put("data", data);
        } else
            map.put("code", Code.PARAM_ERROR);
        return map;
    }



    /**
     * @param reportId Integer 案件id
     * @param comment   String 督办备注
     * @return Map
     * @deprecation 处置督办
     * @author 雷楚桥
     * @date 2018-08-13 14:24
     */
    @ResponseBody
    @RequestMapping("/do_ban")
    public Map<String, Object> do_ban(@Param("reportId") Integer reportId,
                                      @Param("comment") String comment) {

        Map<String, Object> map = new HashMap<>();
        System.out.println("lei:" + reportId);
        CtDoBan doBan = entityHelper.createDoBan(reportId, comment, authHelper.thisUserId());

        doBan.setLight(0);
        doBan = reportService.saveDoBan(doBan);
        //改变督办状态
        reportService.updateDoBan(1,reportId);

        map.put("data", doBan);
        map.put("code", Code.SUCCESS);
        return map;
    }
    
    /**
     * @deprecation 获取督办记录列表
     * @param reportId Integer 案件id
     * @return Map
     * @author 雷楚桥
     * @date 2018-08-13 16:41
     */
    @ResponseBody
    @RequestMapping("/recordList")
    public Map<String,Object> recordList(@Param("reportId") Integer reportId){
        Map<String,Object> map = new HashMap<>();
        List<CtDoBan> doBanList = reportService.findAllDoBanByReportIdOrderByTimeDesc(reportId);
        List<CtDoBanFormat> doBanFormatList = formatHelper.ctDoBanFormat(doBanList);

        map.put("code", Code.SUCCESS);
        map.put("data",doBanFormatList);
        return map;
    }

    /**
     * @author 何平
     * @deprecation 地图所有案件信息
     * @date 14:51 2018/8/2
     * @return java.util.List<com.city.domain.city.CityEvent>
     **/
    @ResponseBody
    @RequestMapping("/findAllReport")
    public List<CtReport> findAll(){
        return reportService.findAllReportByScheduleLimit(1,7);
    }

    /**
     * @author 何平
     * @deprecation 地图最新消息
     * @date 14:51 2018/8/2
     * @return java.util.List<com.city.domain.city.CityEvent>
     **/
    @ResponseBody
    @RequestMapping("/lastMsg")
    public List<CtReportFormat> lastMsg(){
        List<CtReport> list = reportService.findAllReportByScheduleLimit(1,7);
        return formatHelper.ctReportFormat(list);
    }

    @PostMapping("/findBranchPerson")
    @ResponseBody
    public Map<String,Object> findBranchPerson(){
        Map<String,Object> map = new HashMap<>();
        //通过部门id 拿到部门人员
        List<CtUserFoemat> userList = ctUserFoematRepository.findByBranchId(authHelper.thisBranchId());
//        List<PubUser> userList = pubUserService.findByBranchId(authHelper.thisBranchId());
        map.put("code",Code.SUCCESS);
        map.put("data",userList);
        return map;
    }

    /**apply_together
     * @deprecation 发出申请授权请求
     * @param reportId Integer 案件id
     * @param content String 授权理由
     * @author 雷楚桥
     * @date 2018-09-12 2:22
     */
    @ResponseBody
    @RequestMapping("/apply_auth")
    public Map<String,Object> apply_auth(@Param("reportId") Integer reportId,
                                         @Param("content") String content){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();


        CtMessage ctMessage = new CtMessage();
        System.out.println(reportId+"========"+content);
        ctMessage.setAcceptId(0);
        ctMessage.setRemarks(content);
        ctMessage.setTaskId(reportId);
        ctMessage.setSendId(authHelper.thisUserId());
        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ctMessage.setCreateTime(sfd.format(new Date()));
        ctMessage.setType("申请延期");
        messageS.save(ctMessage);
//        data.put("message",messageS.saveMessage(reportId,content,"apply_auth"));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","操作成功");
        return map;
    }

    /**
     * @deprecation 发出申请会办请求
     * @param reportId Integer 案件id
     * @param content String 会办理由
     * @author 雷楚桥
     * @date 2018-09-12 2:22
     */
    @ResponseBody
    @RequestMapping("/apply_together")
    public Map<String,Object> apply_together(@Param("reportId") Integer reportId,
                                             @Param("content") String content){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

//        data.put("message",messageS.saveMessage(reportId,content,"apply_together"));
        CtMessage ctMessage = new CtMessage();
        ctMessage.setAcceptId(0);
        ctMessage.setRemarks(content);
        ctMessage.setTaskId(reportId);
        ctMessage.setSendId(authHelper.thisUserId());
        SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ctMessage.setCreateTime(sfd.format(new Date()));
        ctMessage.setType("申请会办");
        messageS.save(ctMessage);
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","操作成功");
        return map;
    }


}
